<template>
  <div>
    <BreadCrumbView :breadList="breadList"></BreadCrumbView>

    <div class="contentBox">
      <div class="search">
        <SearchView @getValue="getValue">
          <el-button type="primary" slot="btn" @click="addGoods">添加商品</el-button>
        </SearchView>
      </div>
      <el-table stripe :data="tableData" border style="width: 100%">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="600">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column prop="add_time" width="160" label="创建时间">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template v-slot:default="scope">
            <div>
              <el-button @click="editClick(scope.row)" type="primary">编辑</el-button>
              <el-button @click="removeClick(scope.row)" type="danger">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <PageView :total="total"></PageView>
    </div>
  </div>
</template>

<script>
import api from "@/api/goods/goods";
import PageView from "@/components/PageView.vue";
import BreadCrumbView from "../../../components/BreadCrumbView.vue";
import SearchView from "@/components/SearchView.vue";
const dayjs = require("dayjs");
export default {
  name: "GoodsView",
  data() {
    return {
      name: "tutu",
      tableData: [],
      total: 0,
      flag: true,
      value: ''
    };
  },
  computed: {
    breadList() {
      return ["商品管理", "商品列表"];
    },
  },
  created() {
    this.getList();
  },
  mounted() {
    this.$bus.$on('changeSize', (val) => {
      console.log(val);
    })
    this.$bus.$on('changeNum', async (val) => {
      if (this.flag) {
        const res = await api.getGoodsApi(val, 10);
        this.tableData = res.goods;
      } else {
        const res = await api.searchGoods(this.value, val, 10);
        this.tableData = res.goods;
      }
    })
  },
  methods: {
    // TODO获取搜索框的值
    async getValue(val) {
      this.value = val;
      const res = await api.searchGoods(val, 1, 10);
      this.tableData = res.goods;
      this.total = res.total;
      this.flag = false;
    },
    addGoods() {
      this.$router.push({ name: 'addgoods' })
    },
    editClick(val) {
      console.log(val);
    },
    // TODO获取商品列表
    async getList() {
      const res = await api.getGoodsApi(1, 10);
      res.goods.forEach((ele) => {
        ele.add_time = dayjs.unix(ele.add_time).format("YYYY-MM-DD HH:mm:ss");
      });
      // TODO存储总数和数据
      this.tableData = res.goods;
      this.total = res.total;
    },
    async removeClick(val) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          api.deleteGoddsApi(val.goods_id).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  components: { PageView, BreadCrumbView, SearchView },
};
</script>

<style lang="scss" scoped>
.contentBox {
  box-shadow: 0px 0px 13px -6px #000;
  margin-top: 20px;
  position: relative;
  box-sizing: border-box;
  padding: 20px;

  .search {
    width: 600px;
    margin-bottom: 20px;
  }
}
</style>